<?php

//简单管理页面范例，在你的管理页面引用coresection/checkA.php就OK，当然，不要忘了添加注销登录的链接
include_once '../common/script/check_session.php';
?>


<?
include_once("../../config_controller.php");
$host = $_SERVER['HTTP_HOST']; //取得进入所输入的域名
$theme_folder = host_theme($host);

include_once '../../front/' . $theme_folder . '/connection.php';include_once '../common/script/check_session.php';

?>


<!-- 读取前台的config,，用来获取板块显示隐藏  -->
<?php include_once("function.php"); ?>
<?php include '../../front/' . $theme_folder . '/config.php'; ?>


<?php include_once ROOT_PATH_.'/application/phpqrcode/phpqrcode.php'; ?>

<?php

$site_info = select_one_line_all_items($table_Prefix."site"," nid = 0 ");

//传递过来的页面信息。返回的时候，会根据这些信息返回原来的页面

$system_part  = addslashes($_REQUEST ['sys_part']);


$link_code = $_REQUEST['attr']['link_code'];

$detail_attr_list = json_decode($_REQUEST['detail_attr'],true);  //关联文章信息处理


foreach($detail_attr_list as $val){
	
	$sqlstr="select nid,url, name , model ,introduce from ".$table_Prefix."detail where  belong = 'product' and nid = '".$val['value']."'  ";
	$result = mysql_query($sqlstr);
	$goods_info = mysql_fetch_assoc($result); 
	
	$attr = '';
	$jump_url = '';
	$url  = '';
	$domin = str_replace('/', '', $site_info['domin']);
	$jump_attr = '';
	if($_REQUEST['attr']['name'] && $goods_info['name'] != ''){
		$attr.=$attr != '' ? $link_code : '';
		$attr .= 'name:'.$goods_info['name'];
	}
			
	if($_REQUEST['attr']['model'] && $goods_info['model'] != ''){
		$attr.=$attr != '' ? $link_code : '';
		$attr .= 'model:'.$goods_info['model'];
	}
	
	if($_REQUEST['attr']['introduce'] && $goods_info['introduce'] != ''){
		$attr.=$attr != '' ? $link_code : '';
		$attr .= 'introduce:'.$goods_info['introduce'];
	}
			
	if($_REQUEST['attr']['link']  != ''){
		$jump_url = $_REQUEST['attr']['link'];
	}
	
	
	if($_REQUEST['link_attr']['name'] && $goods_info['name'] != ''){
		$jump_attr.=$jump_attr != '' ? $link_code : '';
		$jump_attr .= $goods_info['name'];
	}
			
	if($_REQUEST['link_attr']['model'] && $goods_info['model'] != ''){
		$jump_attr.=$jump_attr != '' ? $link_code : '';
		$jump_attr .= $goods_info['model'];
	}
	
	if($_REQUEST['link_attr']['introduce'] && $goods_info['introduce'] != ''){
		$jump_attr.=$jump_attr != '' ? $link_code : '';
		$jump_attr .= $goods_info['introduce'];
	}
	
	if($_REQUEST['attr']['url'] == 'on'){
		
		
		$url = $domin.'/'.$goods_info['url'].'.html';
		
	}
	elseif($attr != ''){
		
		$url = $attr;
	}
	
	elseif($_REQUEST['attr']['link'] != '' ){
		
		$jump_attr_url = $jump_attr != '' ? '?attr='.$jump_attr : '';
		$url = $_REQUEST['attr']['link'].$jump_attr_url;
	}
	
	//容错级别
    $errorCorrectionLevel = 'L';
    $matrixPointSize = 10;//生成图片大小
	
	$name=iconv("UTF-8","Gb2312",$goods_info['name'].$goods_info['model']);
	
    $img_name = ROOT_PATH_.'/uploadfiles/'.$theme_folder.'/ercode/' . $name . '_qrcode.png';
	
	
	//生成二维码 -- 具体参数见下面说明
    QRcode::png($url, $img_name, $errorCorrectionLevel, $matrixPointSize, 2);
   
    $QR = $img_name;//已经生成的原始二维码图
	
	$str=iconv("Gb2312","UTF-8","中文");
	//输出图片
   imagepng($QR, $img_name);
	
    $img_arr[] = $img_name;

}


$zip_name = ROOT_PATH_.'/uploadfiles/'.$theme_folder.'/ercode_' . date('YmdHis') . '.zip';
	makeZip($zip_name, $img_arr);
	download($zip_name);
	
function makeZip($file_name, $file_list)
    {
        if (file_exists($file_name)) {
            unlink($file_name);
        }
        //重新生成文件
        $zip = new ZipArchive();
        if ($zip->open($file_name, ZIPARCHIVE::CREATE) !== TRUE) {
            exit('无法打开文件，或者文件创建失败');
        }
        foreach ($file_list as $val) {
            if (file_exists($val)) {
                $zip->addFile($val, get_basename($val));
            }
        }
        $zip->close();//关闭
        if (!file_exists($file_name)) {
            exit('无法找到文件'); //即使创建，仍有可能失败
        }
    }


function get_basename($filename){    
         return preg_replace('/^.+[\\\\\\/]/', '', $filename);    
 }   


function download($file)
    {
        if (file_exists($file)) {
            header('Content-Description: File Transfer');
            header('Content-Type: application/octet-stream');
            header('Content-Disposition: attachment; filename=' . basename($file));
            header('Content-Transfer-Encoding: binary');
            header('Expires: 0');
            header('Cache-Control: must-revalidate');
            header('Pragma: public');
            header('Content-Length: ' . filesize($file));
            ob_clean();
            flush();
            readfile($file);
            exit;
        }
}
?>